package br.com.gsc.vaadin;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import br.com.gsc.model.tableMapping.Admin;
import br.com.gsc.model.tableMapping.Company;
import br.com.gsc.model.tableMapping.Operator;
import br.com.gsc.model.tableMapping.Person;
import br.com.gsc.model.tableMapping.Product;
import br.com.gsc.model.tableMapping.User;
import br.com.gsc.repository.objRepos.CompanyRepository;
import br.com.gsc.repository.objRepos.PersonRepository;
import br.com.gsc.repository.objRepos.ProductRepository;
import br.com.gsc.setup.AuthenticationHolder;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Upload;
import com.vaadin.ui.VerticalLayout;

@Configurable(preConstruction=true)
@Transactional
public class AddPowerUser extends CustomComponent implements Button.ClickListener{

	@AutoGenerated
	private AbsoluteLayout mainLayout;
	@AutoGenerated
	private Panel panel_2;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;
	@AutoGenerated
	private VerticalLayout verticalLayout_3;
	@AutoGenerated
	private Button button_1;
	@AutoGenerated
	private ListSelect listSelectEmpresas;
	@AutoGenerated
	private Label label_13;
	@AutoGenerated
	private ListSelect listSelectProdutos;
	@AutoGenerated
	private Label label_12;
	@AutoGenerated
	private VerticalLayout verticalLayout_2;
	@AutoGenerated
	private Upload upload;
	@AutoGenerated
	private Label label_11;
	@AutoGenerated
	private TextField emailField;
	@AutoGenerated
	private Label label_10;
	@AutoGenerated
	private ComboBox comboBox;
	@AutoGenerated
	private Label label_9;
	@AutoGenerated
	private PasswordField passwordField;
	@AutoGenerated
	private Label label_8;
	@AutoGenerated
	private TextField nameField;
	@AutoGenerated
	private Label label_7;


	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */


	@Autowired
	PersonRepository pRepo;
	@Autowired
	ProductRepository proRepo;
	@Autowired
	CompanyRepository cRepo;
	
	@Autowired
	private AuthenticationHolder auth;
	
	private Person person;

	private List<Company> listOfComps;
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	/**
	 * 
	 */
	private static final long serialVersionUID = 8376941444887437182L;
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public AddPowerUser() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		person = pRepo.findPersonByID(auth.getAuthentication().getName());
		comboBox.addItem("User");
		comboBox.addItem("Operator");
		comboBox.addListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				
			}
		});	
		button_1.addListener(this);	
		listSelectEmpresas.setMultiSelect(true);
		listSelectProdutos.setMultiSelect(true);
		if(person instanceof Admin && ((Admin)person).isSuperAdmin() ){
			comboBox.addItem("Admin");
			listSelectProdutos.setEnabled(false);
			listOfComps = cRepo.findAll();
			for(Company c : listOfComps){
				listSelectEmpresas.addItem(c);
			}									
		}else{
			listSelectEmpresas.setEnabled(false);
			for(Company c : person.getListOfCompanies()){
				listSelectEmpresas.addItem(c);
				for(Product p : c.getListOfProducts()){
					listSelectProdutos.addItem(p);
				}
			}
		}
		
	}


	@SuppressWarnings("unchecked")
	public void buttonClick(ClickEvent event) {
		String choice = (String) comboBox.getValue();
		if(choice.equals("User")){
			User user = new User();
			user.setUsername((String)this.nameField.getValue());
			user.setPassword((String)this.passwordField.getValue());
			user.setEmail((String)this.emailField.getValue());
			pRepo.addPerson(user);
		}else if(choice.equals("Operator")){
			Operator oper = new Operator();
			oper.setUsername((String)this.nameField.getValue());
			oper.setPassword((String)this.passwordField.getValue());
			oper.setEmail((String)this.emailField.getValue());
			for(Product pro : (Set<Product>)listSelectProdutos.getValue()){
				Product product = proRepo.findTProductByID(pro.getId());
				oper.addProduct(product);
			}
			for(Company comp : (Set<Company>)listSelectEmpresas.getValue()){
				Company company = cRepo.findTCompanyByID(comp.getId());
				oper.addCompany(company);
			}
			pRepo.addPerson(oper);
		}else if(choice.equals("Admin")){
			Admin adm = new Admin();
			adm.setUsername((String)this.nameField.getValue());
			adm.setPassword((String)this.passwordField.getValue());
			adm.setEmail((String)this.emailField.getValue());
			for(Company comp : (Set<Company>)listSelectEmpresas.getValue()){
				Company company = cRepo.findTCompanyByID(comp.getId());
				adm.addCompany(company);
			}
			pRepo.addPerson(adm);
		}
		getWindow().showNotification("Usuario Gravado!");		
	}


	@AutoGenerated
	private AbsoluteLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new AbsoluteLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("800px");
		mainLayout.setHeight("500px");
		mainLayout.setMargin(true);
		
		// top-level component properties
		setWidth("800px");
		setHeight("500px");
		
		// panel_2
		panel_2 = buildPanel_2();
		mainLayout.addComponent(panel_2,
				"top:0.0px;right:0.0px;bottom:0.0px;left:0.0px;");
		
		return mainLayout;
	}


	@AutoGenerated
	private Panel buildPanel_2() {
		// common part: create layout
		panel_2 = new Panel();
		panel_2.setImmediate(false);
		panel_2.setWidth("100.0%");
		panel_2.setHeight("100.0%");
		
		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		panel_2.setContent(horizontalLayout_1);
		
		return panel_2;
	}


	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("100.0%");
		horizontalLayout_1.setHeight("100.0%");
		horizontalLayout_1.setMargin(true);
		horizontalLayout_1.setSpacing(true);
		
		// verticalLayout_2
		verticalLayout_2 = buildVerticalLayout_2();
		horizontalLayout_1.addComponent(verticalLayout_2);
		horizontalLayout_1.setExpandRatio(verticalLayout_2, 1.0f);
		
		// verticalLayout_3
		verticalLayout_3 = buildVerticalLayout_3();
		horizontalLayout_1.addComponent(verticalLayout_3);
		horizontalLayout_1.setExpandRatio(verticalLayout_3, 1.0f);
		
		return horizontalLayout_1;
	}


	@AutoGenerated
	private VerticalLayout buildVerticalLayout_2() {
		// common part: create layout
		verticalLayout_2 = new VerticalLayout();
		verticalLayout_2.setStyleName("grid_left");
		verticalLayout_2.setImmediate(false);
		verticalLayout_2.setWidth("100.0%");
		verticalLayout_2.setHeight("100.0%");
		verticalLayout_2.setMargin(true);
		verticalLayout_2.setSpacing(true);
		
		// label_7
		label_7 = new Label();
		label_7.setStyleName("lbl_small");
		label_7.setImmediate(false);
		label_7.setWidth("-1px");
		label_7.setHeight("-1px");
		label_7.setValue("Nome");
		verticalLayout_2.addComponent(label_7);
		
		// nameField
		nameField = new TextField();
		nameField.setStyleName("stl-text-field");
		nameField.setImmediate(false);
		nameField.setWidth("285px");
		nameField.setHeight("-1px");
		nameField.setSecret(false);
		verticalLayout_2.addComponent(nameField);
		
		// label_8
		label_8 = new Label();
		label_8.setStyleName("lbl_small");
		label_8.setImmediate(false);
		label_8.setWidth("-1px");
		label_8.setHeight("-1px");
		label_8.setValue("Password");
		verticalLayout_2.addComponent(label_8);
		
		// passwordField
		passwordField = new PasswordField();
		passwordField.setStyleName("stl-text-field");
		passwordField.setImmediate(false);
		passwordField.setWidth("285px");
		passwordField.setHeight("-1px");
		verticalLayout_2.addComponent(passwordField);
		
		// label_9
		label_9 = new Label();
		label_9.setStyleName("lbl_small");
		label_9.setImmediate(false);
		label_9.setWidth("-1px");
		label_9.setHeight("-1px");
		label_9.setValue("Tipo");
		verticalLayout_2.addComponent(label_9);
		
		// comboBox
		comboBox = new ComboBox();
		comboBox.setStyleName("stl-text-field");
		comboBox.setImmediate(false);
		comboBox.setWidth("285px");
		comboBox.setHeight("-1px");
		verticalLayout_2.addComponent(comboBox);
		
		// label_10
		label_10 = new Label();
		label_10.setStyleName("lbl_small");
		label_10.setImmediate(false);
		label_10.setWidth("-1px");
		label_10.setHeight("-1px");
		label_10.setValue("E-mail");
		verticalLayout_2.addComponent(label_10);
		
		// emailField
		emailField = new TextField();
		emailField.setStyleName("stl-text-field");
		emailField.setImmediate(false);
		emailField.setWidth("285px");
		emailField.setHeight("-1px");
		emailField.setSecret(false);
		verticalLayout_2.addComponent(emailField);
		
		// label_11
		label_11 = new Label();
		label_11.setStyleName("lbl_small");
		label_11.setImmediate(false);
		label_11.setWidth("-1px");
		label_11.setHeight("-1px");
		label_11.setValue("Avatar");
		verticalLayout_2.addComponent(label_11);
		
		// upload
		upload = new Upload();
		upload.setStyleName("upload_button");
		upload.setImmediate(false);
		upload.setWidth("-1px");
		upload.setHeight("-1px");
		verticalLayout_2.addComponent(upload);
		
		return verticalLayout_2;
	}


	@AutoGenerated
	private VerticalLayout buildVerticalLayout_3() {
		// common part: create layout
		verticalLayout_3 = new VerticalLayout();
		verticalLayout_3.setStyleName("grid_right");
		verticalLayout_3.setImmediate(false);
		verticalLayout_3.setWidth("100.0%");
		verticalLayout_3.setHeight("100.0%");
		verticalLayout_3.setMargin(false);
		
		// label_12
		label_12 = new Label();
		label_12.setStyleName("lbl_small");
		label_12.setImmediate(false);
		label_12.setWidth("-1px");
		label_12.setHeight("-1px");
		label_12.setValue("Produtos");
		verticalLayout_3.addComponent(label_12);
		
		// listSelectProdutos
		listSelectProdutos = new ListSelect();
		listSelectProdutos.setStyleName("stl-text-field2");
		listSelectProdutos.setImmediate(false);
		listSelectProdutos.setWidth("90.0%");
		listSelectProdutos.setHeight("156px");
		verticalLayout_3.addComponent(listSelectProdutos);
		
		// label_13
		label_13 = new Label();
		label_13.setStyleName("lbl_small");
		label_13.setImmediate(false);
		label_13.setWidth("-1px");
		label_13.setHeight("-1px");
		label_13.setValue("Empresas");
		verticalLayout_3.addComponent(label_13);
		
		// listSelectEmpresas
		listSelectEmpresas = new ListSelect();
		listSelectEmpresas.setStyleName("stl-text-field2");
		listSelectEmpresas.setImmediate(false);
		listSelectEmpresas.setWidth("90.0%");
		listSelectEmpresas.setHeight("-1px");
		verticalLayout_3.addComponent(listSelectEmpresas);
		
		// button_1
		button_1 = new Button();
		button_1.setStyleName("btn_save");
		button_1.setCaption("Salvar");
		button_1.setImmediate(true);
		button_1.setWidth("150px");
		button_1.setHeight("-1px");
		verticalLayout_3.addComponent(button_1);
		
		return verticalLayout_3;
	}
}
